package bman.game.battletank;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

import bman.game.engine.ImageLibrary;

/**
 * Run this class to start BattleTank. 
 * <p>
 * The image directory needs to be in the class path. 
 * For example if the images are in /JavaTools/images then the class path
 * should contain /JavaTools/ (For example: java -cp .;/JavaTools;)
 * <p>
 * The port where the Radio will listen will be dynamic. It will be displayed
 * on the game screen when the Game starts. Clients who which to connect
 * to the the game needs to know this port in order for them to connect.
 * <p>
 * To begin the game one player (lets call this player1) needs to start the game using UI menu File>Start.
 * The next player (player2) should connect to player1. The next player (player3) could either o player2 or
 * player1. The next player (player4) could connect to player1, player2 or player3. etc, etc.
 * 
 * As of Nov15,2008, the game is not complete. When everyone is killed the game ends (toinks)
 * 
 * @author MrJacky
 *
 */
public class Battle extends javax.swing.JFrame implements ActionListener
{

	private JMenuItem helpMenuItem;
	private JMenuItem connectMenu;

	private JMenu jMenu5;

	private JMenuItem startMenuItem;

	private JMenu jMenu3;

	private JMenuBar jMenuBar1;
	
	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				Battle inst = new Battle();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public Battle()
	{
		super();
		initGUI();
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
	}

	GamePanel gp;
	
	private void initGUI()
	{
		try
		{
			gp = new GamePanel();
			this.add(gp);
			
			setSize(gp.getWidth()+10,gp.getHeight()+70);
			{
				jMenuBar1 = new JMenuBar();
				setJMenuBar(jMenuBar1);
				{
					jMenu3 = new JMenu();
					jMenuBar1.add(jMenu3);
					jMenu3.setText("File");
					{
						startMenuItem = new JMenuItem();
						jMenu3.add(startMenuItem);
						startMenuItem.setText("Start");
						startMenuItem.addActionListener(gp);

					}
					{
						connectMenu = new JMenuItem();
						jMenu3.add(connectMenu);
						connectMenu.setText("Connect to a server");
						connectMenu.addActionListener(gp);
					}
				}
				{
					jMenu5 = new JMenu();
					jMenuBar1.add(jMenu5);
					jMenu5.setText("Help");
					{
						helpMenuItem = new JMenuItem();
						jMenu5.add(helpMenuItem);
						helpMenuItem.setText("Help");
					}
				}
			}
			
			this.validate();
			this.addKeyListener(gp);
			setFocusable(true);
			
			Component[] c = this.getComponents();
			for(int i = 0; i < c.length; i++)
			{
				c[i].setFocusable(false);
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{
		
		String cmd = e.getActionCommand();
	    System.out.println("Source: "+cmd);
	    
	    try
		{
	    	   String s = (String)JOptionPane.showInputDialog(
                       this, 
                       "Connect to a Battle Tank Server....\n" +
                       "Enter the IP address and port: \n " +
                       "For example 127.0.0.1:777",
                       "127.0.0.1:777");
	    	  System.out.println(s);
	    	
		}
		catch (Exception e1)
		{
			e1.printStackTrace();
		}
	}
}
